package com.controller;

import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.entity.User;
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
//import com.mapper.UserXmlMapper;



@Controller
//@RequestMapping("/view")
public class ViewController extends WebMvcConfigurerAdapter{
	//@Autowired
	//private UserXmlMapper userXmlMapper;
	
	//测试web的get请求
	@GetMapping("/testGet")
    public String testGet(Model model) {
        model.addAttribute("user", new User("123","末日使者","999"));
        return "index";
    }
	
	//测试web的post请求
    @PostMapping("/testPost")
    public String testPost(@ModelAttribute User user) {
        return "result";
    }

    /*//测试分页
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value = "/testPage")
    public String testPage(Model model) {
  		List<User> userList=userXmlMapper.getAll();
  		//获取第1页，10条内容，默认查询总数count
  		PageHelper.startPage(1, 2);
  		//用PageInfo对结果进行包装
  		PageInfo page = new PageInfo(userList);
  		//测试PageInfo全部属性
  		//PageInfo包含了非常全面的分页属性
  		page.setPageNum(1);
  		page.setPageSize(1);
  		page.setStartRow(1);
  		page.setEndRow(1);
  		page.setTotal(183);
  		page.setPages(19);
  		page.setFirstPage(1);
  		page.setLastPage(8);
  		page.setIsFirstPage(true);
  		page.setIsLastPage(false);
  		page.setHasPreviousPage(false);
  		page.setHasNextPage(true);
  		model.addAttribute("userList", userList);
  		model.addAttribute("page", page);
  		return "page";
    }*/
    
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/results").setViewName("form-success");
    }

    @GetMapping("/form")
    public String showForm(User user) {
        return "form";
    }

    //表单验证
    @PostMapping("/form")
    public String checkPersonInfo(@Valid User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "form";
        }
        return "redirect:/results";
    }
        
}
	
	
	
	
